package com.vegetable.modules.controller.stock;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vegetable.common.annotation.SysLog;
import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.stock.PriceAlertRecord;
import com.vegetable.modules.entity.stock.po.PriceAlertRecordQueryPO;
import com.vegetable.modules.service.stock.IPriceAlertRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 价格预警记录表 前端控制器
 * </p>
 *
 * @author qoder
 * @since 2025-09-11
 */
@Slf4j
@RestController
@RequestMapping("/priceAlertRecord")
@AllArgsConstructor
@Api(tags = "价格预警记录")
public class PriceAlertRecordController extends AbstractController {

    private final IPriceAlertRecordService priceAlertRecordService;

    @SysLog(value = "查看价格预警记录列表")
    @PostMapping("/list")
    @ApiOperation(value = "获取价格预警记录列表", notes = "分页获取价格预警记录列表")
//    @RequiresPermissions("stock:price:alert:record:list")
    public R<Page<PriceAlertRecord>> list(@RequestBody PriceAlertRecordQueryPO po) {
        
        log.info("用户{}请求价格预警记录列表，商品名称：{}，状态：{}，分类ID：{}，页码：{}，每页数量：{}, 预警日期: {}",
                getUserId(), po.getCommodityNameLike(), po.getAlertStatus(), po.getCategoryId(), po.getPage(), po.getSize(), po.getOrderTime());
        
        try {
            Page<PriceAlertRecord> pageParam = new Page<>(po.getPage(), po.getSize());
            Page<PriceAlertRecord> result = priceAlertRecordService.page(pageParam, po);
            return R.data(result);
        } catch (Exception e) {
            log.error("获取价格预警记录列表失败", e);
            return R.fail("获取价格预警记录列表失败：" + e.getMessage());
        }
    }

    @SysLog(value = "统计未处理价格预警记录数量")
    @GetMapping("/countUnhandled")
    @ApiOperation(value = "统计未处理价格预警记录数量", notes = "统计未处理价格预警记录数量")
    public R<Integer> countUnhandled() {
        
        log.info("用户{}请求统计未处理价格预警记录数量", getUserId());
        
        try {
            PriceAlertRecordQueryPO po = new PriceAlertRecordQueryPO();
            po.setAlertStatus(0); // 0未处理
            int count = priceAlertRecordService.count(po);
            return R.data(count);
        } catch (Exception e) {
            log.error("统计未处理价格预警记录数量失败", e);
            return R.fail("统计未处理价格预警记录数量失败：" + e.getMessage());
        }
    }

}